package demo;
//import com.mysql.jdbc.Driver;

import java.sql.*;
import java.sql.DriverManager;

public class FirstJdbc {
    public static void main(String[] args) {
        //类内部把自己注册了一下
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        //连接字符串
        String url = "jdbc:mysql://localhost:3306/java68?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, "root", "root1234");
            System.out.println(con.getClass().getName());//接口实现类
            st = con.createStatement();
            rs = st.executeQuery("select * from s_subject");
            while (rs.next()) {
                //>>>rs.getObject()
                long id = rs.getLong("id");
                long grade = rs.getLong("gradeid");
                String name = rs.getString("subjectname");
                int classHour = rs.getInt("classhour");
                System.out.printf("%d,%d,%s,%d\n", id, grade, name, classHour);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (st != null)
                    st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
